package com.smedi.ismedi.authoritymanagement.core.infrastructure.converter;

import com.github.dozermapper.core.CustomConverter;
import com.github.dozermapper.core.MappingException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: xulinjun
 * @Date: 2021-6-22 14:30
 */
public class DataScopeConverter implements CustomConverter {
    @Override
    public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
        if (sourceFieldValue == null) {
            return new ArrayList();
        }
        if (sourceFieldValue instanceof HashMap) {
            Map<String, List<Long>> destinations = new HashMap<>();
            Map<String, List<Long>> maps = (Map<String, List<Long>>) sourceFieldValue;
            if (maps.size() > 0) {
                for (Map.Entry<String, List<Long>> entry : maps.entrySet()) {
                   destinations.put(entry.getKey(), entry.getValue());
                }
                return destinations;
            } else {
                return new HashMap<String, List<Long>>();
            }
        } else {
            throw new MappingException("Converter DataScopeConverter Error: "
                    + existingDestinationFieldValue
                    + " and "
                    + destinationClass);
        }
    }
}
